package com.whisperarts.financer.app.dialogs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.forms.widgets.FormToolkit;

import com.whisperarts.financer.app.model.rcp.AccountsContentProvider;
import com.whisperarts.financer.app.model.rcp.AccountsLabelProvider;
import com.whisperarts.financer.model.account.Account;

/**
 * @author AD
 *
 */
public class SelectAccountDialog extends Dialog {

	
	private FormToolkit toolkit;

	protected List<Button> buttons = new ArrayList<Button>();

	private final List<Account> accounts = new ArrayList<Account>();

	private Account account;

	private Button okButton;

	private TreeViewer viewer;

	private final IWorkbenchWindow window;
	

	public SelectAccountDialog(IWorkbenchWindow window) {
		super(window.getShell());
		this.window = window;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Select account");
		newShell.setSize(280, 370);
		Monitor primary = newShell.getDisplay().getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = newShell.getBounds();
	    
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    
	    
	    newShell.setLocation(x, y);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		
		toolkit = new FormToolkit(parent.getDisplay());
		
		Composite composite = toolkit.createComposite(parent);
        composite.setLayout(new GridLayout(1, false));
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		viewer = new TreeViewer(composite, SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		
		
		viewer.setAutoExpandLevel(2);		
		viewer.setContentProvider(new AccountsContentProvider());
		viewer.setLabelProvider(new AccountsLabelProvider());
		viewer.setInput(new Object());
		
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				if(event.getSelection() instanceof IStructuredSelection) {
					IStructuredSelection selection = (IStructuredSelection)event.getSelection();
					if (selection.getFirstElement() instanceof Account) {
						okButton.setEnabled(true);
						account = (Account)selection.getFirstElement();
						return;
					}
				}
				okButton.setEnabled(false);
			}
		});
		viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
		return composite;
	}

	@Override
	protected Button createButton(Composite parent, int id, String label,
			boolean defaultButton) {
		if (id == IDialogConstants.OK_ID) {
			okButton = super.createButton(parent, id, label, defaultButton);
			okButton.setEnabled(false);
			return okButton;
		}
	    return super.createButton(parent, id, label, defaultButton);

	}
	
	@Override
	protected void okPressed() {		
		super.okPressed();
	}

	public Account getAccount() {
		return account;
	}	
	
}
